package com.lz.football.module.friend
{
	import com.common.stdlib.ArrayUtil;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.FriendEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.friend.vo.FriendVo;
	import com.lz.football.module.friend.item.FriendItem;
	
	import common.BaseView.BaseView;
	import common.BaseView.DisplayMouseEventManager;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.nick.PagePanel;
	
	/**
	 *好友主面板
	 * @author Administrator
	 * 
	 */
	public class FriendView extends BaseView implements IFriendView
	{
		/**
		 *添加好友按钮 
		 */
		private var addFriendBtn:SimpleButton;
		/**
		 *推荐好友按钮  
		 */
		private var recommendBtn:SimpleButton;
		/**
		 *申请加入好友数量 
		 */
		private var tipNumber:TextField;
		/**
		 *喇叭 
		 */
		private var horn:MovieClip;
		/**
		 *列表容器 
		 */
		private var listContain:MovieClip;
		/**
		 *换页容器 
		 */
		private var pageContain:MovieClip;
		/**
		 * 
		 */
		private var _page:PagePanel;
		/**
		 *好友item 
		 */
		private var _itemVec:Vector.<FriendItem>;
		/**
		 *系统消息面板 
		 */
		private var _messageView:FriendMessageView;
		/**
		 *推荐面板 
		 */
		private var _recommendView:FriendRecommendView;
		
		/**
		 * 输入名称申请添加好友
		 */
		private var _addFriendView:AddFriendView;
		
		public function FriendView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.football.module.friend.mainView");
			this.addChild(symbolSkin);
			_windowBg = symbolSkin.friendBg;
			isDrag = true;
			setCloseBtn(symbolSkin.closeBtn);
			
			addFriendBtn = symbolSkin.addFriendBtn;
			recommendBtn = symbolSkin.recommendBtn;
			tipNumber = symbolSkin.tipNumber;
			horn = symbolSkin.horn;
			listContain = symbolSkin.listContain;
			pageContain = symbolSkin.pageContain;
			tipNumber.mouseEnabled = false;
//			super.initUI();
			_viewWidth = this.width;
			_viewHeight = this.height;
			initItem();
			tipNumber.text = "";
			tipNumber.text = AppData.getInstance().friendManager.invitedfriend <=0 ? "":""+AppData.getInstance().friendManager.invitedfriend; 
			onResizeHandler();
			addEvtListener();
			DisplayMouseEventManager.instance.addTipsDisplay(horn,"系统消息");
		}
		override public function onResizeHandler(event:Event=null):void
		{
			this.y = 100
			this.x = (GlobalDef.StageWidth - _viewWidth);
		}
		
		/**
		 *初始化 
		 * 
		 */
		private function initItem():void
		{
			var item:FriendItem;
			_itemVec = new Vector.<FriendItem>();
			for(var i:int = 0;i < 8;i++)
			{
				item = new FriendItem();
				item.initUI();
				listContain.addChild(item);
				item.y = (item.height ) * i;
				item.x = 2;
				_itemVec.push(item);
				item.callBack = selectItem;
			}
			item = new FriendItem();
			item.initUI(symbolSkin.gmMc);
			_page = new PagePanel();
			_page.initUI(symbolSkin.page);
			this.addChild(_page);
			_page.callBack = updataList;
		}
		/**
		 *选择了好友  
		 * @param $item
		 * @return 
		 * 
		 */
		private function selectItem($item:FriendItem):void
		{
			DriftView.instance.friendVo = $item.data as FriendVo
		}
		override public function onCloseHandler(event:MouseEvent):void
		{
			super.onCloseHandler(event);
			this.dispatchEvent(new FriendEvent(ModuleEvent.REMOVE_MODULE));
			
		}
		override public function addEvtListener():void
		{
			super.addEvtListener();
			addFriendBtn.addEventListener(MouseEvent.CLICK,onAddFriendHandler);
			recommendBtn.addEventListener(MouseEvent.CLICK,onrecommendHandler);
			horn.addEventListener(MouseEvent.CLICK,onreceiveInviteHandler);
			AppEventDispatcher.getInstance().addEventListener(NotifierEvent.FRIEND_DATA_CHANGE,friendNumHandler);
		}
		override public function removeEvtListener():void
		{
			super.removeEvtListener();
			addFriendBtn.removeEventListener(MouseEvent.CLICK,onAddFriendHandler);
			recommendBtn.removeEventListener(MouseEvent.CLICK,onrecommendHandler);
			horn.removeEventListener(MouseEvent.CLICK,onreceiveInviteHandler);
			AppEventDispatcher.getInstance().removeEventListener(NotifierEvent.FRIEND_DATA_CHANGE,friendNumHandler);
			DisplayMouseEventManager.instance.removeTipsDisplay(horn);
		}
		/**
		 *接收到好友的邀请 
		 * @param event
		 * 
		 */
		protected function friendNumHandler(event:NotifierEvent):void
		{
			// TODO Auto-generated method stub
			var num:int = event.params as int;
			tipNumber.text = ""+ event.params;
			tipNumber.text = num<=0 ? "":""+num; 
			if(_messageView != null && _messageView.parent)
			{
				_messageView.updataList();
			}
		}
		
		protected function onreceiveInviteHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			if(AppData.getInstance().friendManager.inviteList == null)
			{
				AppEventDispatcher.getInstance().dispatchEvent(new FriendEvent(FriendEvent.OPEN_RECEIVE_PANEL));
			}
			else
			{
				getreceive_invite(null);
			}
		}
		
		/**
		 * 推荐 
		 * @param event
		 * 
		 */
		protected function onrecommendHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			
			AppEventDispatcher.getInstance().dispatchEvent(new FriendEvent(FriendEvent.RECOMMEND_LIST));
			updataInviteList(null);
			
		}
		
		/**
		 *添加好友  
		 * @param event
		 * 
		 */
		protected function onAddFriendHandler(event:MouseEvent):void
		{
			// TODO Auto-generated method stub
			
			
			if(_addFriendView == null)
			{
				_addFriendView = new AddFriendView();
				_addFriendView.initUI();
			}
			GlobalDef.GameWindowContainer.addChild(_addFriendView);
//			_addFriendView.onResizeHandler();
//			AppEventDispatcher.getInstance().addEventListener(FriendEvent.RECOMMEND_LIST,onRecommendList);
			
		}
		/**
		 *名称邀请返回 
		 * @param $obj
		 * 
		 */
		public function nameInvite($obj:Object):void
		{
			if(_addFriendView != null && _addFriendView.parent)
				_addFriendView.nameInvite($obj);
		}
		/**
		 *更新好友列表  
		 * 
		 */
		public function updataList($str:String = "left"):void
		{
			resetItem();
			var arr:Array = AppData.getInstance().friendManager.friendList;
			_page.max = Math.ceil(arr.length/8);
			if(_page.min > _page.max)
				_page.min = _page.max;
			var tempArr:Array = ArrayUtil.getPageSizeDatas(_page.min,8,arr);
			var len:int = tempArr.length > _itemVec.length? _itemVec.length : tempArr.length;
			for(var i:int = 0;i < len;i++)
			{
				_itemVec[i].data = tempArr[i];
				_itemVec[i].callBack = selectItem;
			}
			
			
		}
		/**
		 *重置item 
		 * 
		 */
		private function resetItem():void
		{
			for each(var item:FriendItem in _itemVec)
			{
				item.reset();
			}
		}
		/**
		 *更新好友列表 
		 * type 为1则需要更新好友列表 2为不需要更新好友列表 同时更新邀请列表 {status:true,type:1}
		 * @param $obj
		 * 
		 */
		public function updataFriendList($obj:Object):void
		{
			if($obj.type == 1)
			{
				updataList();
			}
			if(_messageView != null && _messageView.parent)
			{
				_messageView.updataList();
			}
			tipNumber.text = AppData.getInstance().friendManager.invitedfriend <=0 ? "":""+AppData.getInstance().friendManager.invitedfriend; 
		}
		/**
		 * 推荐面板
		 * @param $obj
		 * 
		 */
		public function updataInviteList($obj:Object):void
		{
			if(_recommendView == null)
			{
				_recommendView = new FriendRecommendView();
				_recommendView.initUI();
			}
			if(!_recommendView.parent)
			{
				GlobalDef.GameWindowContainer.addChild(_recommendView);
				_recommendView.onResizeHandler();
				
			}
			_recommendView.updataList();
			
		}
		/**
		 *返回受到邀请的列表 
		 * @param $obj
		 * 
		 */
		public function getreceive_invite($obj:Object):void
		{
			if(_messageView == null)
			{
				_messageView = new FriendMessageView();
				_messageView.initUI();
			}
			GlobalDef.GameWindowContainer.addChild(_messageView);
			_messageView.onResizeHandler();
			_messageView.updataList();
		}
		/**
		 * 
		 * 
		 */
		override public function destroyView():void
		{
			removeEvtListener();
			_page.destroyView();
			while(_itemVec.length)
			{
				_itemVec.pop().destroyView();
			}
			_itemVec = null;
			if(_messageView != null)
				_messageView.destroyView();
			if(_recommendView != null)
				_recommendView.destroyView();
			if(_addFriendView != null)
				_addFriendView.destroyView();
			
			_page = null;
			super.destroyView();
		}
	}
}